
//
//  LoginView.m
//  afn
//
//  Created by mac on 2018/5/8.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "LoginView.h"

@implementation LoginView

-(void)creatView{
    
    UILabel *label=[UILabel new];
    [self addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(50);
    }];
    label.text=@"免密登录";
    label.font=[UIFont systemFontOfSize:16];
    label.textColor=ColorWithRGB(49, 49, 49);
    
    
    UIView *xianView=[UIView new];
    [self addSubview:xianView];
    [xianView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.bottom.mas_equalTo(self.mas_centerY).mas_equalTo(-70);
        make.height.mas_equalTo(1);
    }];
    xianView.backgroundColor=_LightGrey;
    
    _codeText=[UITextField new];
    [self addSubview:_codeText];
    [_codeText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(xianView);
        make.right.mas_equalTo(-120);
        make.bottom.mas_equalTo(xianView.mas_top).mas_equalTo(0);
        make.height.mas_equalTo(50);
    }];
    _codeText.placeholder=@"验证码";
    _codeText.font=[UIFont systemFontOfSize:15];
    _codeText.returnKeyType=UIReturnKeyDone;
    _codeText.delegate=self;
    
    _codeBnt=[UIButton new];
    [self addSubview:_codeBnt];
    [_codeBnt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.centerY.mas_equalTo(self->_codeText);
        make.width.mas_equalTo(90);
        make.height.mas_equalTo(30);
    }];
    [_codeBnt setTitle:@"获取验证码" forState:0];
    [_codeBnt setTitleColor:ColorWithRGB(49, 49, 49) forState:0];
    _codeBnt.titleLabel.font=[UIFont boldSystemFontOfSize:11];
     _codeBnt.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
     [_codeBnt addTarget:self action:@selector(huoQuYanZhen:) forControlEvents:UIControlEventTouchUpInside];
    
    
    UIView *xianView2=[UIView new];
    [self addSubview:xianView2];
    [xianView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.bottom.mas_equalTo(self->_codeText.mas_top).mas_equalTo(-15);
        make.height.mas_equalTo(1);
    }];
    xianView2.backgroundColor=_LightGrey;
    
    _phoneText=[UITextField new];
    [self addSubview:_phoneText];
    [_phoneText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(xianView2);
        make.right.mas_equalTo(-120);
        make.bottom.mas_equalTo(xianView2.mas_top).mas_equalTo(0);
        make.height.mas_equalTo(50);
    }];
    _phoneText.placeholder=@"手机号";
    _phoneText.font=[UIFont systemFontOfSize:15];
    _phoneText.keyboardType=UIKeyboardTypePhonePad;
    
    
    UIButton *LoginBtn=[UIButton new];
    [self addSubview:LoginBtn];
    [LoginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(xianView);
        make.top.mas_equalTo(self.mas_centerY).mas_equalTo(-15);
        make.height.mas_equalTo(50);
    }];
    LoginBtn.backgroundColor=_backgroundColor;
    [LoginBtn setTitleColor:[UIColor whiteColor] forState:0];
    [LoginBtn setTitle:@"登 录" forState:0];
    LoginBtn.layer.cornerRadius=25;
    LoginBtn.titleLabel.font=[UIFont systemFontOfSize:16];
    [LoginBtn addTarget:self action:@selector(Register:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *WXLoginBtn=[UIButton new];
    [self addSubview:WXLoginBtn];
    [WXLoginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(xianView);
        make.top.mas_equalTo(LoginBtn.mas_bottom).mas_equalTo(15);
        make.height.mas_equalTo(50);
    }];
    WXLoginBtn.layer.borderWidth=1;
    WXLoginBtn.layer.borderColor=_LightGrey.CGColor;
    [WXLoginBtn setTitleColor:ColorWithRGB(116, 176, 74) forState:0];
    [WXLoginBtn setTitle:@"微信登录" forState:0];
    WXLoginBtn.layer.cornerRadius=25;
    WXLoginBtn.titleLabel.font=[UIFont systemFontOfSize:16];
    
    
    UILabel *label2=[UILabel new];
    [self addSubview:label2];
    [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.bottom.mas_equalTo(-30);
    }];
    [label layoutIfNeeded];
    
    NSMutableAttributedString * attributedStr = [[NSMutableAttributedString alloc] initWithString:@"登陆代表你已同意《法之春用户协议》"];
    [attributedStr addAttribute:NSForegroundColorAttributeName value:ColorWithRGB(149, 149, 149) range:NSMakeRange(0, 8)];
    [attributedStr addAttribute:NSForegroundColorAttributeName value:ColorWithRGB(99, 161, 252) range:NSMakeRange(8, 9)];
    label2.attributedText=attributedStr;
    label2.font=[UIFont systemFontOfSize:11];
    
    UIImageView *imageView=[UIImageView new];
    [self addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(label2.mas_left).mas_equalTo(-5);
        make.centerY.mas_equalTo(label2);
        make.width.height.mas_equalTo(13);
    }];
    imageView.image=[UIImage imageNamed:@"ico-gouxuan-on"];
}


-(IBAction)huoQuYanZhen:(UIButton *)sender{
    
    NSString *mobile=_phoneText.text;
    if (![gongju isMobileNumber:mobile]) {
        [gongju svpInfo:@"手机号错误"];
        return;
    }
    NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:mobile,@"mobile",@"app_login",@"type", nil];
    [gongju AFNPOSTDataUrl:@"v1/smscode/get-sms-code" andDic:dic andTableView:nil andViewController:self.viewController andSuccessBlock:^(id responseObject) {
        self->_codeBnt.enabled=NO;
        self->_timeTick=60;
        self->_timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES];
    } anderrorBlock:nil];
    
}

-(IBAction)Register:(UIButton *)sender{
    NSString *mobile=_phoneText.text;
    if (![gongju isMobileNumber:mobile]) {
        [gongju svpInfo:@"手机号错误"];
        return;
    }
    NSString *codeStr=_codeText.text;
    if ([gongju charIsNil:codeStr]) {
        [gongju svpInfo:@"验证码不能为空"];
        return;
    }
    NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:mobile,@"mobile",codeStr,@"code", nil];
    [gongju AFNPOSTDataUrl:@"v1/login/mobile" andDic:dic andTableView:nil andViewController:self.viewController andSuccessBlock:^(id responseObject) {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setObject:[responseObject objectForKey:@"token"] forKey:@"token"];
        
    } anderrorBlock:nil];
    
    
}


-(void)timeFireMethod
{
    if (_timeTick==0) {
        _codeBnt.enabled=YES;
        [_codeBnt setTitle:@"发送验证码" forState:0];
        [_codeBnt setTitleColor:ColorWithRGB(49, 49, 49) forState:0];
        [_timer invalidate];
        return;
    }
    _timeTick--;
    [_codeBnt setTitle:[NSString stringWithFormat:@"%dS重发",_timeTick] forState:0];
    [_codeBnt setTitleColor:ColorWithRGB(149, 149, 149) forState:0];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string{
    if ([[[UITextInputMode currentInputMode]primaryLanguage] isEqualToString:@"emoji"]) {
        return NO;
    }
    return YES;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

@end
